import {
	getFixedWarehousing,
	doChange
} from "@/views/wy-asset-management/change/api/change.js";
import assetDictionary from "@/views/wy-asset-management/mixin/assetDictionary";
import { mapGetters } from "vuex";
import { getDict } from "~/utils";
export default {
	mixins: [assetDictionary],
	data() {
		return {
			// 是否是政府项目
			government: 0,
			// 建筑面原始值
			landArea: "",
			loading: true,
			//基本附件
			busId: this.$route.query.assetFixedWarehousingId,
			modelType: "asset-warehousing",
			fileList: [],
			//变更附件
			changeBusId: "",
			changeFileList: [],
			//入库附件
			wbusId: this.$route.query.warehousingAttachment,
			wfileList: "",

			//弹出框 初始时隐藏
			formObj: {
				//基本信息
				roomId: "",
				assetFixedWarehousingId: this.$route.query.assetFixedWarehousingId,
				warehousingAttachment: "",
				assetName: "",
				assetClassification: 0,
				useStatus: 0,
				acquisitionMethod: 0,
				acquisitionDate: "",
				investmentAcquisitionDate: "",
				valueType: 0,
				registrationPrice: 0,
				registrationFee: 0,
				taxation: 0,
				value: 0,
				number: "",
				measurementUnit: 0,
				landArea: "",
				propertyRightForm: 0,
				ownershipCertificateNo: "",
				issueData: "",
				ownershipPeriod: "",
				ownershipCertificate: 0,
				location: "",
				ownershipNature: 0,
				selfUseArea: 0,
				selfUseValue: 0,
				buildingStructure: 0,
				assetCode: "",
				voucherNo: "",
				voucherDateStart: "",
				voucherDateEnd: "",
				assetRegistrationId: "",
				assetFixedWarehousingLogList: [],
				assetRegistrationList: [],
				assetChangeRecordVoList: [],
				assetChangeRecordVo: {},
				changeRemarks: "",
				warehouseRemark: "",
				changeId: "",
				//变更通过状态 0 不通过 1 通过
				changeType: 0
			},
			formObj2: {
				assetCode2: "",
				voucherNo2: "",
				voucherDate: [],
				warehouseRemark: ""
			},
			//变更描述
			formObj3: {
				changeRemarks3: ""
			},
			rules: {
				landArea: [
					{
						required: true,
						message: "请输入建筑/土地面积",
						trigger: "blur"
					}
				]
			},
			rules3: {
				changeRemarks3: [
					{
						required: false,
						message: "请填写备注信息",
						trigger: "blur"
					},
					{
						min: 0,
						max: 100,
						message: "长度在0-100个字符串",
						trigger: "blur"
					}
				]
			}
		};
	},
	computed: {
		...mapGetters({
			cityAssembly: "getCityAssembly",
			districtCollection: "getDistrictCollection"
		})
	},
	/**
	 * 载入事件
	 */
	mounted() {
		//根据主键查询已入库的信息
		this.getWarehousing();
	},
	methods: {
		getDict(v) {
			return getDict(v);
		},
		/**
		 * 根据主键查询已入库的信息
		 */
		getWarehousing() {
			this.loading = true;
			getFixedWarehousing(this.$route.query.assetFixedWarehousingId)
				.then(resp => {
					let obj = resp.data.data;
					this.formObj = obj;
					this.government = this.formObj.assetRegistrationId == "1" ? "1" : "0";
					this.formObj2.assetCode2 = obj.assetCode;
					this.formObj2.voucherNo2 = obj.voucherNo;
					this.formObj2.voucherDate[0] = obj.voucherDateStart;
					this.formObj2.voucherDate[1] = obj.voucherDateEnd;
					this.landArea = this.formObj.landArea;
					this.formObj2.warehouseRemark = obj.warehouseRemark;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "获取信息失败"
					});
				})
				.finally(() => (this.loading = false));
		},

		/**
		 * 保存按钮 触发文件上传
		 */
		sendData(val) {
			this.formObj.changeType = val;
			if (this.formObj.changeType !== 0) {
				this.rules3.changeRemarks3[0].required = !val;
				if (this.formObj.landArea == this.landArea)
					return this.$message({
						message: "请对建筑 / 土地面积”进行调整",
						type: "warning"
					});
			}
			if (!this.formObj3.changeRemarks3)
				this.formObj3.changeRemarks3 = val == 0 ? "变更不通过" : "变更通过";
			this.loading = true;
			this.$refs["ruleForm"].validate(valid => {
				if (valid) this.$refs.warehousingFile.submit(this.modelType);
				else return (this.loading = false);
			});
		},

		/**
		 * 保存表单信息
		 * @returns {Promise<void>}
		 */
		saveEntity(res, file, fileList) {
			console.log(res.data);
			if (res.data != null) this.formObj.changeId = res.data;
			this.formObj.assetCode = this.formObj2.assetCode2;
			this.formObj.voucherNo = this.formObj2.voucherNo2;
			this.formObj.voucherDateStart = this.formObj2.voucherDate[0];
			this.formObj.voucherDateEnd = this.formObj2.voucherDate[1];
			this.formObj.changeRemarks = this.formObj3.changeRemarks3;
			let formData = this.formObj;
			doChange(formData)
				.then(data => {
					this.$message({
						type: "success",
						message: "操作成功"
					});
					//操作完成 跳转页面
					this.loading = false;
					this.$router.push("/asset-management/change-list");
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "操作失败"
					});
				})
				.finally(() => (this.loading = false));
		}
	}
};
